using UnityEngine;

public class OrbitCamera : MonoBehaviour
{
	public GameObject Target;

	public float distance = 5f;

	public float xSpeed = 120f;

	public float ySpeed = 120f;

	public float yMinLimit = -20f;

	public float yMaxLimit = 80f;

	public float distanceMin = 0.5f;

	public float distanceMax = 15f;

	private float x;

	private float y;

	private void Start()
	{
		Vector3 eulerAngles = base.transform.eulerAngles;
		x = eulerAngles.y;
		y = eulerAngles.x;
	}

	public void Aim(Vector2 direction)
	{
		x += direction.x * xSpeed * distance * 0.02f;
		y -= direction.y * ySpeed * 0.02f;
	}

	private void LateUpdate()
	{
		if (Target != null)
		{
			y = ClampAngle(y, yMinLimit, yMaxLimit);
			Quaternion rotation = Quaternion.Euler(y, x, 0f);
			Vector3 point = new Vector3(0f, 0f, 0f - distance);
			Vector3 position = rotation * point + Target.transform.position;
			base.transform.rotation = rotation;
			base.transform.position = position;
		}
	}

	public static float ClampAngle(float angle, float min, float max)
	{
		if (angle < -360f)
		{
			angle += 360f;
		}
		if (angle > 360f)
		{
			angle -= 360f;
		}
		return Mathf.Clamp(angle, min, max);
	}
}
